package com.example.tbs;

import android.os.Bundle;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.tbs.databinding.ActivityMainBinding;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

public class MainActivity extends BaseActivity {
    ActivityMainBinding binding;
    String downloadUrl;//预览的时候根据这个url确定的文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        init();
    }

    private void init() {
        downloadUrl = binding.etMainInput.getText().toString();

        binding.btnMainPermission.setOnClickListener(v -> {
            PermissionUtils.permissionGroup(PermissionConstants.STORAGE)
                    .callback(new PermissionUtils.SimpleCallback() {
                        @Override
                        public void onGranted() {
                            ToastUtils.showShort("权限申请成功");
                        }

                        @Override
                        public void onDenied() {
                            ToastUtils.showShort("权限申请失败");
                        }
                    })
                    .request();
        });
        binding.btnMainDownload.setOnClickListener(v -> {
            if (PermissionUtils.isGranted(PermissionConstants.STORAGE)) {
                downloadUrl = binding.etMainInput.getText().toString();
                download(downloadUrl);
            } else {
                binding.btnMainPermission.performClick();
            }

        });
        binding.btnMainPreview.setOnClickListener(v -> {
            if (PermissionUtils.isGranted(PermissionConstants.STORAGE)) {
                FileDisplayActivity.launch(pContent, downloadUrl);
            } else {
                binding.btnMainPermission.performClick();
            }
        });
    }

    private int count = 0;
    private void download(String url) {
        WaitingDialog.showLoading(pContent, "加载中");
        OkHttpUtils.get().url(url).build().execute(new FileCallBack(FileDisplayUtils.getCacheDir(url).toString(), FileDisplayUtils.getFileName(url)) {
            @Override
            public void inProgress(float progress, long total, int id) {
                super.inProgress(progress, total, id);
                count++;
                if (count > 200) {
                    LogUtils.v("inProgress"+progress);
                    count = 0;
                }
                WaitingDialog.changeMessage("加载中...(" + (int) (progress * 100) + "%)");
            }

            @Override
            public void onError(okhttp3.Call call, Exception e, int id) {
                LogUtils.d("文件下载失败");
                ToastUtils.showShort("文件加载失败");
                File file = FileDisplayUtils.getCacheFile(url);
                if (!file.exists()) {
                    LogUtils.d("删除下载失败文件");
                    file.delete();
                }
                WaitingDialog.stopLoading();
            }

            @Override
            public void onResponse(File response, int id) {
                ToastUtils.showShort("文件加载成功");
                ///storage/emulated/0/007/928a61644c3f420079818aee016a21a6.docx
                LogUtils.v("onResponse :" + response.getAbsolutePath());
                WaitingDialog.stopLoading();
            }


        });
    }
}